package com.lianxueda.drawing.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lianxueda.drawing.common.CreateGroup;
import com.lianxueda.drawing.common.R;
import com.lianxueda.drawing.common.UpdateGroup;
import com.lianxueda.drawing.domain.dto.LeaveQueryDTO;
import com.lianxueda.drawing.domain.dto.WxLeaveFormDTO;
import com.lianxueda.drawing.domain.pojo.WxLeave;
import com.lianxueda.drawing.domain.vo.LeaveVo;
import com.lianxueda.drawing.domain.vo.PageVO;
import com.lianxueda.drawing.mapper.StudentsMapper;
import com.lianxueda.drawing.service.WxLeaveService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 29264
 */
@RestController
@Slf4j

@RequestMapping("/api/v1/wxLeave")
@Tag(name = "请假管理模块", description = "请假管理模块接口")
@RequiredArgsConstructor
public class WxLeaveController {

    @Resource
    private WxLeaveService wxLeaveService;
    @Resource
    private StudentsMapper studentsMapper;

    @Operation(summary = "获取全部请假信息")
    @GetMapping("/list")
    public R list(){
        List<WxLeave> list = wxLeaveService.list();
        return R.suc(list);
    }


    @Operation(summary = "新增请假信息")
    @PostMapping("/add")
    public R createLeave(@Validated(CreateGroup.class) @RequestBody WxLeaveFormDTO leaveFormDTO){
        WxLeave leave = new WxLeave();
        BeanUtils.copyProperties(leaveFormDTO, leave);
        wxLeaveService.save(leave);
        return R.suc("新增请假成功");
    }

    @Operation(summary = "删除请假信息")
    @DeleteMapping("/delete/{id}")
    public R deleteById(@PathVariable(value = "id", required = true)Integer id) {
        WxLeave leave = wxLeaveService.getById(id);
        if (leave == null) {
            return R.fail("请假信息不存在");
        }
        wxLeaveService.removeById(id);
        return R.suc("请假信息删除成功");
    }

    @Operation(summary = "修改请假信息")
    @PutMapping("/update")
    public R updateLeave(@Validated(UpdateGroup.class) @RequestBody WxLeaveFormDTO leave) {
        WxLeave leavePo = new WxLeave();
        BeanUtils.copyProperties(leave, leavePo);
        wxLeaveService.updateById(leavePo);
        return R.suc("请假信息更新成功");
    }


    @Operation(summary = "根据id查询")
    @GetMapping("/select/{id}")
    public R getLeaveById(@PathVariable(value = "id",required = true) Integer id) {
        WxLeave leave = wxLeaveService.getById(id);
        if (leave != null) {
            return R.suc(leave);
        } else {
            return R.fail("未找到对应的请假记录");
        }
    }

    @Operation(summary = "分页查询")
    @PostMapping("/page")
    public R page(@RequestBody LeaveQueryDTO leaveQueryDTO) {
        IPage<WxLeave> page = new Page<>(leaveQueryDTO.getCurPage(), leaveQueryDTO.getPageSize());

        LambdaQueryWrapper<WxLeave> qw = new LambdaQueryWrapper<>();
        qw.select(WxLeave.class, info -> !info.getColumn().equals("deleted"));
        qw.orderByDesc(WxLeave::getId);
        //3.查询
        IPage<WxLeave> pageResult = wxLeaveService.page(page, qw);
        List<WxLeave> records = pageResult.getRecords();
        //遍历结果
        for(WxLeave leave:records){
            Integer studentId = leave.getStudentId();
            String studentName =studentsMapper.getStudentNameBy(studentId);
            leave.setStudentName(studentName);
        }

        PageVO<WxLeave> pageVo = new PageVO<>();
        pageVo.setTotal(pageResult.getTotal());
        pageVo.setList(pageResult.getRecords());
        pageVo.setPageSize(pageResult.getSize());
        pageVo.setCurrentPage(pageResult.getCurrent());
        return R.suc(pageVo);
    }

    @Operation(summary = "修改请假状态")
    @PutMapping("/updateStatus/{id}/{status}")
    public R updateLeaveStatus(@PathVariable(value = "id", required = true) Integer id,
                               @PathVariable(value = "status", required = true) Integer status) {
        WxLeave leave = wxLeaveService.getById(id);
        if (leave == null) {
            return R.fail("请假信息不存在");
        }
        leave.setStatus(status); // 设置新的状态
        boolean isUpdated = wxLeaveService.updateById(leave);
        if (isUpdated) {
            return R.suc("请假状态更新成功");
        } else {
            return R.fail("请假状态更新失败");
        }
    }

    @GetMapping ("/pageList")
    @Operation(summary = "分页查询请假")
    public R LeavePage(@RequestParam(defaultValue = "1") Integer page ,@RequestParam(defaultValue = "5")Integer PageSize){
         LeaveVo leaveVo = wxLeaveService.selectLeavePage(page,PageSize);

         return R.suc(leaveVo);
    }

}
